//
//  HJPreferenceSettingController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJPreferenceSettingController.h"
#import "HJCodeGuideCollectionCell.h"
#import "HJCodeGuideListCell.h"
#import "HJMineModel.h"

static NSString * reuseIdentifier = @"CollectionCell";
@interface HJPreferenceSettingController ()
@property(nonatomic,strong)HJUserModel * model;
@property(nonatomic,strong)NSMutableString * brandid;
@property(nonatomic,strong)NSNumber* size;


@property(nonatomic,strong) NSMutableArray * brandIDArr;

@end

@implementation HJPreferenceSettingController

-(NSMutableArray *)brandIDArr {
    if (!_brandIDArr) {
        _brandIDArr = [NSMutableArray array];
    }
    return _brandIDArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.model = [[HJUserModel alloc]init];
    self.brandid =[NSMutableString string];
    [self loadNetworkData];
    
}

-(void)configTableView {
    self.tableViewStyle = UITableViewStyleGrouped;
    [super configTableView];
    CGFloat H;
    if (mScreenHeight<812) {
        H = mScreenHeight -60 -64;
    }else {
        H = mScreenHeight-60-TabbarHeight-X_Bottom_Space;
    }
    self.tableView.frame = CGRectMake(0, 0, mScreenWidth, H);
    [self.tableView registerCellByNibName:@"HJCodeGuideListCell"];
    [self configBottom];
}

-(void)loadNetworkData {
    [TZHttpTool post:PreferenceAPI params:nil success:^(NSDictionary *result) {
        self.model = [HJUserModel mj_objectWithKeyValues:result[@"data"]];
        
        for (brandListModel *list in _model.brand_list) {
            if ([list.check isEqual:@1]) {
                [self.brandIDArr addObject:list.brand_id];
            }
        }
        for (defaultSizeModel *size in _model.default_size) {
            if ([size.check isEqual:@1]) {
                self.size = size.size_num.numberValue;
            }
        }
        [self.tableView reloadData];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    }
    return _model.brand_list.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCodeGuideCollectionCell * collectionCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!collectionCell) {
        collectionCell = [[HJCodeGuideCollectionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    if (indexPath.section==0) {
        collectionCell.sizeBlock = ^(NSString *size) {
            self.size = size.numberValue;
        };
        collectionCell.models = _model.default_size;
        return collectionCell;
    }else {
        HJCodeGuideListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"HJCodeGuideListCell"];
        listCell.selectionStyle = UITableViewCellSelectionStyleNone;
        brandListModel *model = _model.brand_list[indexPath.row];
        listCell.sneakersName.text = model.brand_name;
        listCell.brand = model.brand_id.integerValue;
        if ([model.check isEqual:@1]) {
            listCell.btn.selected = YES;
        }else {
            listCell.btn.selected = NO;
        }
        return listCell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        return 80;
    }
    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [self configSectionHeaderWithTitleArr:@[@"您常穿的尺码是多少", @"您感兴趣的品牌(选填)"] section:section];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==1) {
        HJCodeGuideListCell * cell = (HJCodeGuideListCell *)[tableView cellForRowAtIndexPath:indexPath];
        brandListModel *model = _model.brand_list[indexPath.row];
        cell.btn.selected = !cell.btn.selected;
        if (cell.btn.selected) {
            model.check = @1;
            [self.brandIDArr addObject:model.brand_id];
        }else {
            model.check = @0;
            [self.brandIDArr removeObject:model.brand_id];
        }
    }
}
-(UIView *)configSectionHeaderWithTitleArr:(NSArray *)titleArr section:(NSInteger)section {
    UIView *sectionView = [[UIView alloc]init];
    sectionView.frame = CGRectMake(0, 0, mScreenWidth, 60);
    sectionView.backgroundColor = [UIColor whiteColor];
    
    UILabel * lable = [[UILabel alloc]init];
    lable.frame = CGRectMake(25, sectionView.centerY, mScreenWidth-25, 22);
    lable.text = titleArr[section];
    lable.font = HJFontSize(17);
    lable.textAlignment = NSTextAlignmentLeft;
    lable.textColor = HJColorRGB(0);
    [sectionView addSubview:lable];
    
    return sectionView;
}

-(void)configBottom {
    CGFloat H;
    if (mScreenHeight<812) {
        H = mScreenHeight -60 -64;
    }else {
        H = mScreenHeight-60-TabbarHeight-X_Bottom_Space;
    }
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, H, mScreenWidth, 60);
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:HJColorRGB(0) forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [btn setBackgroundColor:HJColorRGB(255)];
    btn.layer.shadowOpacity = 0.2;
    btn.layer.shadowRadius = 3;
    btn.layer.shadowOffset = CGSizeZero;
    btn.layer.shadowColor = [UIColor blackColor].CGColor;
    [btn addTarget:self action:@selector(okBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [self.view bringSubviewToFront:btn];
}
-(void)okBtn:(UIButton *)btn {
    
    if (self.size.floatValue==0.0) {
        [self showHint:@"请选择尺码"];
        return;
    }
    NSMutableDictionary * params =[NSMutableDictionary dictionary];
    for (NSString * str in self.brandIDArr) {
        [self.brandid appendFormat:@"%@,",str];
    }
    if (self.brandid.length < 1) {
        [self showHint:@"请选择品牌"];return;
    }
    NSString * str = [self.brandid substringToIndex:self.brandid.length-1];
    params[@"interest_brand"] = str;
    params[@"size"] = self.size;
    [TZHttpTool post:AfterRegister params:params success:^(NSDictionary *result) {
        [mUserDefaults setObject:[NSString stringWithFormat:@"%@码",self.size] forKey:@"often_size"];
        [self showHint:@"设置成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}

@end
